package com.demo.controller;
import com.alibaba.fastjson.JSONObject;
import com.demo.bean.User;
import com.demo.mapper.UserMapper;
import com.demo.service.RedisService;
import com.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import java.util.concurrent.TimeUnit;

/**
 * @author 李天航
 * 用户操作接口
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserMapper userMapper;
    @Resource
    UserService userService;
    @Resource
    RedisService redisService;

    /**
     * 用户登录
     * @param userAccount 用户账号
     * @param userPwd 用户密码
     * @param session 本地缓存
     * @return status:操作状态 msg:操作信息
     */
    @PostMapping("/login")
    public Object userLogin(String userAccount, String userPwd, HttpSession session, HttpServletRequest request)
    {
        LinkedHashMap<String, Object> resultMap = new LinkedHashMap<>();

        try {
            User loginUser = userMapper.getUserByUserAccount(userAccount);
            if(loginUser==null) {
                throw new Exception("用户不存在");
            }
            if (loginUser.getUserPwd().equals(userPwd)) {
                resultMap.put("status", 200);
                resultMap.put("msg", "登录成功");
                User rawUser= (User) redisService.getSessionData(session.getId(), "user");
                if(rawUser!=null){
                    redisService.deleteUserSession(rawUser.getUserAccount());
                }
                redisService.setSessionData(session.getId(),"user",loginUser);
                redisService.setUserSessionId(userAccount,session.getId());
                redisService.setSessionData(session.getId(),"userAccount",userAccount);
            } else {
                resultMap.put("status", 500);
                resultMap.put("msg", "密码错误");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status", 500);
            resultMap.put("msg", e.getMessage());
        }
        return resultMap;
    }

    @PostMapping("/getnow")
    public Object getNowUser(HttpSession session)
    {
        User nowUser=(User)  redisService.getSessionData(session.getId(),"user");
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        resultMap.put("status",200);
        resultMap.put("user",nowUser);
        return resultMap;
    }

    /**
     * 登出
     * @param session 本地缓存，不用提供
     * @return status:操作状态 msg:操作信息
     */
    @PostMapping("/logout")
    public LinkedHashMap<String, Object> userLogout(HttpSession session)
    {
        User user = (User)  redisService.getSessionData(session.getId(),"user");
        redisService.setSessionData(session.getId(),"user",null);
        LinkedHashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("msg","登出成功");
        resultMap.put("status",200);
        return resultMap;
    }

    /**
     * 添加用户
     * @param user 用户信息
     * @return status:操作状态 msg:操作信息
     */
    @PostMapping("/add")
    public Object userAdd(User user)
    {
        return userService.addUser(user);
    }

    /**
     * 更新用户
     * @param user 更新后的用户信息
     * @param session 本地缓存
     * @return status:操作状态 msg:操作信息
     */
    @PostMapping("/update")
    public Object userUpdate(User user,HttpSession session)
    {
        User loginUser=(User) redisService.getSessionData(session.getId(),"user");
        if(loginUser==null)
        {
            LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
            resultMap.put("status",500);
            resultMap.put("msg","请不要恶作剧");
            return resultMap;
        }
        else {
            if(user.getUserCity()!=null) {
                loginUser.setUserCity(user.getUserCity());
            }
            if(user.getUserSex()!=null) {
                loginUser.setUserSex(user.getUserSex());
            }
            if(user.getUserBirthday()!=null) {
                loginUser.setUserBirthday(user.getUserBirthday());
            }
            if(user.getUserSno()!=null){
                loginUser.setUserSno(user.getUserSno());
            }
            if(user.getUserSnoPwd()!=null){
                loginUser.setUserSnoPwd(user.getUserSnoPwd());
            }
            redisService.setSessionData(session.getId(),"user",loginUser);
            return userService.updateUser(loginUser);
        }
    }

    /**
     * 修改密码
     * @param session 本地缓存
     * @param lastPwd 旧密码
     * @param nowPwd 新密码
     * @return status:操作状态 msg:操作信息
     */
    @PostMapping("/update/pwd")
    public Object userUpdatePwd(HttpSession session,String lastPwd,String nowPwd)
    {
        User loginUser= (User)  redisService.getSessionData(session.getId(),"user");
        LinkedHashMap<String, Object> resultMap=userService.updatePwd(nowPwd,loginUser,lastPwd);
        if(resultMap.get("status").equals("200"))
        {
            loginUser.setUserPwd(nowPwd);
            session.setAttribute("user",loginUser);
        }
        return resultMap;
    }

}
